from tool.es_clent import connect_es, store_data_bulk
import json
from tool.string_tool import clean_des
from tool.es_clent import create_index
import pymysql


def get_user_follow():
    user_datas = json.load(open("../data/user.json"))
    reverse_map = {}
    for user in user_datas:
        if "followers" in user and user["followers"]:
            for follower in user["followers"]:
                if follower[9:] in reverse_map:
                    reverse_map[follower[9:]] = []
                reverse_map[follower[9:]].append(user['url'][40:])
    for user in user_datas:
        if len(reverse_map[user['url'][40:]]) > 0:
            user["follows"] = reverse_map[user['url'][40:]]
        else:
            user["follows"] = []
    with open("../data/new_user.json", 'w') as file:
        json.dump(user_datas, file)


def process_users():
    """
    读取../data中的user.json数据,批量插入es库中
    :return:
    """
    user_datas = json.load(open("../data/user.json"))
    user_map = {}
    for user in user_datas:
        clean_user = {
            "fans_id": [follower[9:] for follower in user["followers"]] if "followers" in user and user["followers"] else [],
            "fans_num": len(user["followers"]) if "followers" in user and user["followers"] else 0,
            "user_id": user["url"][40:],
            "develop_fans": 0,
            "follower_id": []
        }
        user_map[clean_user["user_id"]] = clean_user
    api_datas = json.load(open("../data/apis.json"))

    # 将一个人的所有作品的总follow数添加到自身
    for api in api_datas:
        if not api["developers"]:
            continue
        fans_num = len(api["followers"]) if api["followers"] else 0
        for developer in api["developers"]:
            developer_id = developer["developer_link"][9:]
            if developer_id not in user_map:
                continue
            user_map[developer_id]["develop_fans"] += fans_num

    # 每个人follow了多少人
    for user_id in user_map:
        fans_id = user_map[user_id]["fans_id"]
        for fid in fans_id:
            if fid in user_map:
                user_map[fid]["follower_id"].append(user_id)
    insert_data = [user_map[key] for key in user_map]
    return insert_data



if __name__ == "__main__":
    # es = connect_es()
    # es.indices.delete(index="user")
    # create_index(es, "user", "../data/es_index/user.json")
    #
    insert_data = process_users()
    # # 存入es库中
    # store_data_bulk(es_object=es, index_name="user", doc_type="user", data_list=insert_data)

    # 存入mysql 中
    cnn = pymysql.connect(host="127.0.0.1",
                          user="daniel",
                          password="123456",
                          database="service_recommend",
                          charset="utf-8")
    cursor = cnn.cursor()
    insert_sql = ""
    cursor.execute()

    # print(es.get(index="mashup", doc_type="mashup", id=1))
